package com.fkd.service.impl;

import com.fkd.dao.UserDao;
import com.fkd.pojo.Order;
import com.fkd.pojo.Product;
import com.fkd.pojo.User;
import com.fkd.pojo.UserAddress;
import com.fkd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User login(User user) {
        return userDao.login(user);
    }

    @Override
    public void register(User user) {
        userDao.register(user);
    }

    @Override
    public int getMyHasPayedOrderCount(User user,int limit) {      //返回订单页数（每页8个订单）
        int count = userDao.getMyHasPayedOrderCount(user.getId());
        int pageCount = count/limit;
        if(count%limit != 0){
            pageCount += 1;
        }
        return pageCount;
    }

    @Override
    public List<Order> getMyHasPayedOrder(User user,int page,int limit) {      //返回当前页订单
        int begin = (page - 1)*limit;    //显示的第一条订单为(当前页-1)*8
        return userDao.getMyHasPayedOrder(user.getId(),begin,limit);           //得到当前订单
    }

    @Override
    public Product getProductDetaile(int productId) {
        return userDao.getProductDetaile(productId);
    }     //返回当前商品的详细信息

    @Override
    public void addToCar(int userId, int productId, int count, int state) {     //加入购物车
        userDao.addToCar(userId,productId,count,state);
    }

    @Override
    public List<Order> getMyProductCar(int userId) {        //获取购物车信息
        List<Order> list = userDao.getMyProductCar(userId);
        return list;
    }

    @Override
    public void deleteCarProduct(int orderId) {
        userDao.deleteCarProduct(orderId);
    }      //删除购物车中的商品

    @Override
    public void updateOrderCount(int orderId, int count) {
        userDao.updateOrderCount(orderId,count);
    }   //更新订单数量

    @Override
    public List<UserAddress> getUserAddress(int userId) {
        return userDao.getUserAddress(userId);
    }    //获取用户所有收货地址信息

    public String getSerialNumber(){           //获取一个随机的10位串行码
        String str ="A1B2C3D4E5F6G7HI8G3K23L9MN0O214PQ2R5ST6UV32W3XY2Z";
        StringBuffer ddh = new StringBuffer();
        int num = 0;
        for(int i = 0;i<10;i++){
            num = (int) (Math.random()*str.length());     //得到一个0-49之间的随机整数
            ddh.append(str.charAt(num));         //获取str中的随机一个字符
        }
        return ddh.toString();    //获取一个由str中的字符组成的随机10位数
    }

    @Override
    public void updateOrder(int addressId, int userId) {     //更新订单信息
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    //时间格式化
        String createTime = simpleDateFormat.format(date);
        userDao.updateOrder(createTime,getSerialNumber(),addressId,userId);
    }

    @Override
    public void addAddress(int userId, String newAddress) {
        userDao.addAddress(userId,newAddress);
    }       //新增用户地址

    @Override
    public int getNewAddressId(int userId, String newAddress) {         //获取新地址id
        return userDao.getNewAddressId(userId,newAddress);
    }

    @Override
    public String getSerialNumber(int orderId) {            //获取订单商品串行码
        return userDao.getSerialNumber(orderId);
    }

    @Override
    public String getOrderAddress(int addressId) {
        return userDao.getOrderAddress(addressId);
    }       //获取订单地址
}
